<?php
	
	function scripts_url()
	{
		$siteURL = "http://".SERVER_PATH;
		return $siteURL;
	}
	
	function site_url()
	{
		$siteURL = scripts_url();
		if(isset($_SESSION["language"]))
		{
			if($_SESSION["language"]["is_default"] != 1)
			{
				$siteURL .= strtolower($_SESSION["language"]["abv"]) ."/";
			}
		}
		return $siteURL;
	}
	
	function get_languages()
	{
		global $connection; 
	 	$languages = $connection->fetch_result("SELECT * FROM languages WHERE active = 1 ORDER BY is_default DESC");
		
		if(isset($_GET["language"]))
		{
			foreach($languages as $key => $value)
			{
				if(strtolower($value["abv"]) == $_GET["language"])
				{
					$found = true;
					$lang = $value;
					unset($languages[$key]);
				}
			}
		}
		
		if(!isset($lang) || $lang == "")
		{
			$lang = $languages[0];
			unset($languages[0]);
		}
		
		$_SESSION["language"] = $lang;
		
		return $languages;
	}
	
	function get_setting($name)
	{
		if(getRequestParameter($name) !== false)
		{
			return getRequestParameter($name);
		}
		
		global $connection;
		$setting = $connection->fetch_row("settings", $name, "name");
		
		return $setting["value"];
	}
	
	function get_settings()
	{
		global $connection;
		$settings = array();
		
		$values = $connection->fetch_table("settings");
		
		foreach($values as $key => $setting)
		{
			if(getRequestParameter($setting["name"]) !== false)
			{
				$value = getRequestParameter($setting["name"]);
			}
			else
			{
				$value = $setting["value"];
			}
			$settigns[$setting["name"]] = $value;
		}
		return $settigns;
	}
	
	function get_translations()
	{
		global $connection; 
		
	 	$values = $connection->fetch_result("SELECT * FROM translates WHERE language_id = ?", array($_SESSION["language"]["id"]));
		
		global $translations;
		$translations = array();
		foreach($values as $key => $value)
		{
			$translations[$value["keyword"]] = $value["text"];
		}
		
		return $translations;
	}
	
	function translate($name)
	{
		global $connection;
		
		global $translations;
		
		if(count($translations) == 0)
		{
			$translations = get_translations();
		}
		
		if(!isset($translations[$name]))
		{
			$retrurnString = "<a style='color:pink;text-transform:uppercase; font-weight:600;' target='_blank' rel='nofollow' href='".scripts_url().WIZARD_PATH."translate-word.html?keyword=".$name."'>translate</a>";
			return $retrurnString;
		}
		return $translations[$name];
	}
	
	function get_page()
	{
		global $connection;
		
		$siteURL = site_url();
		if(!isset($_GET["page"]))
		{
			header("Location:".$siteURL."404.html");
		}
		
		$breadcrumbs = array();
		
		$languageID = $_SESSION["language"]["id"];
		
		$homePage = get_site_page_by_slug("home", $languageID);
		
		$breadcrumbs[] = array("name" => $homePage["name"], "url" => $siteURL);
		
		if($_GET["page"] == "homepage")
		{
			# HOMEPAGE
			
			$page = $homePage;
			$page["url"] = $siteURL;
			$page["meta_title"] = $page["name"];
		}
		elseif($_GET["page"] == "page")
		{
			# PAGE
			
			$pageURL = $_GET["page_url"];
			$page = get_site_page($pageURL, $languageID);
			if(isset($page["id"]))
			{
				$page["url"] = $siteURL.$page["url"].".html";
				
				$breadcrumbs[] = array("name" => $page["name"], "url" => $page["url"]);
				
				$page["meta_title"] = $page["name"];
			}
		}
		elseif(getRequestParameter("page") == "companies" || getRequestParameter("page") == "company")
		{
			$page = get_site_page_by_slug("companies", $languageID);
			
			$pageURL = $siteURL.$page["url"]."/";
			
			$meta_title = $page["name"];
			
			$breadcrumbs[] = array("name" => $page["name"], "url" => $pageURL);
			
			if(getRequestParameter("company"))
			{
				$companyURL = getRequestParameter("company");
				$company = getCompanyByField($companyURL, "url");
				if($company)
				{
					$newPageURL = $pageURL.$company["url"]."/";
					
					$company["url"] = $pageURL.$company["url"].".html";
					
					$pageURL = $newPageURL;
					
					$page = $company;
					$page["url"] = $company["url"];
					$page["children_url"] = $pageURL;
					
					$breadcrumbs[] = array("name" => $page["name"], "url" => $company["url"]);
					
					$meta_title = $page["name"] . " | " . $meta_title;
					
					if(getRequestParameter("super_category"))
					{
						$superCategory = get_category(getRequestParameter("super_category"), $languageID);
						if($superCategory)
						{
							$superCategory["url"] = $pageURL = $pageURL.$superCategory["url"]."/";
							$breadcrumbs[] = array("name" => $superCategory["name"], "url" => $superCategory["url"]);
							
							if(getRequestParameter("category"))
							{
								$category = get_category(getRequestParameter("category"), $languageID);
								if($category)
								{
									$category["url"] = $pageURL = $pageURL.$category["url"]."/";
									$breadcrumbs[] = array("name" => $category["name"], "url" => $category["url"]);
									
									if(getRequestParameter("sub_category"))
									{
										$subCategory = get_category(getRequestParameter("sub_category"), $languageID);
										if($subCategory)
										{
											$subCategory["url"] = $pageURL = $pageURL.$subCategory["url"]."/";
											$breadcrumbs[] = array("name" => $subCategory["name"], "url" => $subCategory["url"]);
										}
									}
								}
							}
						}
					}
				}
			}
			$page["meta_title"] = $meta_title;
		}
		elseif(isset($_GET["super_category"]))
		{
			# CATEGORY PAGE
			
			$meta_title = "";
			
			if(isset($_GET["company"]))
			{
				$companyURL = $_GET["company"];
				$company = getCompanyByField($companyURL, "url");
				if($company)
				{
					$companyPage = get_site_page_by_slug("companies", $languageID);
					$company["url"] = $siteURL.$companyPage["url"]."/".$company["url"]."/";
					$breadcrumbs[] = array("name" => $company["name"], "url" => $company["url"]);
					$meta_title = $company["name"];
				}
			}
			
			$categoryURL = $_GET["super_category"];
			$superCategory = get_category($categoryURL, $languageID);
			if(isset($superCategory["id"]))
			{
				$superCategory["url"] = $categoryURL = $siteURL.$superCategory["url"]."/";
				$page = $superCategory;
				
				$breadcrumbs[] = array("name" => $page["name"], "url" => $page["url"]);
				
				if($meta_title != "")
				{
					$meta_title = " - ". $meta_title;
				}
				$meta_title = $superCategory["name"].$meta_title;
				
				$superCategoryID = $superCategory["id"];
				$categoryID = null;
				$subcategoryID = null;
				
				if(isset($_GET["category"]))
				{
					unset($page);
					# SUBCATEGORY CATEGORY PAGE
					
					$category = get_category($_GET["category"], $languageID);
					if(isset($category["id"]))
					{
						$categoryID = $category["id"];
						$category["url"] = $categoryURL = $categoryURL.$category["url"]."/";
						$page = $category;
						
						$breadcrumbs[] = array("name" => $page["name"], "url" => $category["url"]);
						
						$meta_title = $page["name"] . " - " . $meta_title;
						
						
						if(isset($_GET["sub_category"]))
						{
							unset($page);
							# SUBCATEGORY CATEGORY PAGE
							
							$subCategory = get_category($_GET["sub_category"], $languageID);
							if(isset($subCategory["id"]))
							{
								$subcategoryID = $subCategory["id"];
								$subCategory["url"] = $categoryURL = $categoryURL.$subCategory["url"]."/";
								$page = $subCategory;
								
								$breadcrumbs[] = array("name" => $page["name"], "url" => $subCategory["url"]);
								
								$meta_title = $page["name"] . " - " . $meta_title;
						
								if(isset($_GET["filters"]))
								{
									$filtes = array();
									
									$URLfilters = explode("/", $_GET["filters"]);
									
									foreach($URLfilters as $key => $filter)
									{
										$filterValue = get_property_by_url($filter, $languageID);
										if(isset($filterValue["property_id"]))
										{
											$filters[$filterValue["property_id"]] = $filterValue;
										}
									}
									$page["filters"] = $filters;
								}
							}
						}
					}
					
					if(isset($_GET["product"]))
					{
						unset($page);
						$productURL = $_GET["product"];
						
						$product = getProductByField($productURL,"url", $categoryID, $subcategoryID);
						if(isset($product["id"]))
						{
							if(!isset($_SESSION["history"][$product["id"]]))
							{
								$_SESSION["history"][$product["id"]] = $product["id"];
							}
							$product["url"] = $categoryURL.$product["url"]."html";
							$page = $product;
							$breadcrumbs[] = array("name" => $page["name"], "url" => $page["url"]);
							
							$meta_title = $page["name"] . " - " . $meta_title;
						}
					}
					
				}
				$page["meta_title"] = $meta_title;
			}
		}
		elseif(getRequestParameter("event_page"))
		{
			# EVENT PAGE
			$eventsPage = $page = get_site_page_by_slug("events", $languageID);
			
			
			$breadcrumbs[] = array("name" => $eventsPage["name"], "url" => $siteURL."evenimente/");
			
			$meta_title = $eventsPage["name"];
			
			if(getRequestParameter("event"))
			{
				$eventURL = getRequestParameter("event");
				
				$event = $page = get_event($eventURL, $languageID);
				
				$meta_title = $event["name"] . " - " . $meta_title;
				
				$breadcrumbs[] = array("name" => $event["name"], "url" => $siteURL."evenimente/".$event["url"].".html");
			}
			$page["meta_title"] = $meta_title;
		}
		
		if(!isset($page["id"]))
		{
			//header("Location:".site_url()."404.html");
		}
		
		$page["breadcrumbs"] = $breadcrumbs;
		$page["meta_title"] = generate_meta_title($page["meta_title"]);
		$page["meta_description"] = generate_meta($page["meta_description"]);
		$page["meta_keywords"] = generate_meta_keywords($page["meta_keywords"]);
		
		return $page;
	}
	
	function generate_meta_title($title)
	{
		return $title;
	}
	
	function generate_meta($meta)
	{
		return $meta;
	}
	
	function generate_meta_keywords($meta)
	{
		return $meta;
	}
	
	function summarize_description($description, $character_count)
	{
		$description = trim(strip_tags($description));
		if(strlen($description) > $character_count+1)
		{
			$line=$description;
			if (preg_match('/^.{1,'.$character_count.'}\b/s', $description, $match))
			{
		    	$line=$match[0];
			}
				
			return $line."...";
		}
		
		return $description;
	}
	
	function get_offers()
	{
		global $connection;
		$offers = $connection->fetch_result("SELECT o.*, od.text FROM offers o
													LEFT JOIN offers_details od ON o.id = od.offer_id
												WHERE od.language_id = ?", array($_SESSION["language"]["id"]));
		return $offers;
	}
	
	function getRequestParameter($param)
	{
		if(isset($_POST[$param]))
		{
			return $_POST[$param];
		}
		
		if(isset($_GET[$param]))
		{
			return $_GET[$param];
		}
		
		return false;
	}
	
	function validateEmail($email) {
	  // First, we check that there's one @ symbol, 
	  // and that the lengths are right.
	  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
	    // Email invalid because wrong number of characters 
	    // in one section or wrong number of @ symbols.
	    return false;
	  }
	  // Split it into sections to make life easier
	  $email_array = explode("@", $email);
	  $local_array = explode(".", $email_array[0]);
	  for ($i = 0; $i < sizeof($local_array); $i++) {
	    if
	(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
	↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
	$local_array[$i])) {
	      return false;
	    }
	  }
	  // Check if domain is IP. If not, 
	  // it should be valid domain name
	  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
	    $domain_array = explode(".", $email_array[1]);
	    if (sizeof($domain_array) < 2) {
	        return false; // Not enough parts to domain
	    }
	    for ($i = 0; $i < sizeof($domain_array); $i++) {
	      if
	(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
	↪([A-Za-z0-9]+))$",
	$domain_array[$i])) {
	        return false;
	      }
	    }
	  }
	  return true;
	}
	
?>